package com.youyi.yyscreencutlibrary.Track;

import android.content.Intent;
import android.hardware.display.VirtualDisplay;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;

import com.lmiot.xyclick.Util.LkxLog;
import com.youyi.yyscreencutlibrary.SDK.YYScreenCutSDK;
import com.youyi.yyscreencutlibrary.ShowMainActivity;
import com.youyi.yyscreencutlibrary.Track.YYTrackSDK;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class YYTrackLibActivity extends AppCompatActivity {
    private static final int SCREEN_SHOT = 1;
    private static final String TAG = "YYTrackLibActivity";
    public static Handler handler = new Handler(Looper.getMainLooper());
    private int dpi;
    private MediaProjection.Callback mCallback;
    private int mHeightPixels;
    private VirtualDisplay mVirtualDisplay;
    private int mWidthPixels;

    public int getMax(int i, int i2, int i3) {
        if (i2 > i) {
            i = i2;
        }
        return i3 > i ? i3 : i;
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (YYScreenCutSDK.mIsFinish) {
            EventBus.getDefault().post(new ShowMainActivity(true));
            finish();
            return;
        }
        initSize();
        YYTrackSDK.getInstance().setOnCallDestroyListener(new YYTrackSDK.OnCallDestroyListener() {
            @Override
            public void result(boolean z) {
                YYTrackLibActivity.this.destroyRecord();
            }
        });
        if (!YYTrackSDK.hasPermission) {
            requestPermission();
        } else if (Build.VERSION.SDK_INT >= 29) {
            startForegroundService(new Intent(this, TrackScreenService.class));
        } else {
            startRecording();
        }
    }

    private void initSize() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
        this.mWidthPixels = displayMetrics.widthPixels;
        this.mHeightPixels = displayMetrics.heightPixels;
        this.dpi = displayMetrics.densityDpi;
    }

    private void requestPermission() {
        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getApplicationContext().getSystemService("media_projection");
        YYTrackSDK.projectionManager = mediaProjectionManager;
        startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), 1);
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity
    public void onActivityResult(int i, int i2, Intent intent) {
        if (i == 1 && i2 == -1) {
            YYTrackSDK.hasPermission = true;
            YYTrackSDK.resultCode = i2;
            YYTrackSDK.data = intent;
            if (Build.VERSION.SDK_INT >= 29) {
                startForegroundService(new Intent(this, TrackScreenService.class));
            } else {
                startRecording();
            }
        } else {
            YYTrackSDK.hasPermission = false;
            if (YYTrackSDK.mOnStrartRecordListener != null) {
                YYTrackSDK.mOnStrartRecordListener.result(false, "");
            }
            destroyRecord();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(StartTrackBean startTrackBean) {
        LkxLog.d("YYTrackLibActivity onMessageEvent");
        startRecording();
    }

    private void startRecording() {
        moveTaskToBack(true);
        handler.postDelayed(new Runnable() {
            public void run() {
                try {
                    LkxLog.d("YYTrackLibActivity startRecording projection:"+YYTrackSDK.projection);
                    if (YYTrackSDK.projection == null) {
                        YYTrackSDK.projection = YYTrackSDK.projectionManager.getMediaProjection(YYTrackSDK.resultCode, YYTrackSDK.data);
                        if (YYTrackSDK.projection != null) {
                            YYTrackLibActivity.this.mCallback = new MediaProjection.Callback() {
                                /* class com.youyi.yyscreencutlibrary.Track.YYTrackLibActivity.AnonymousClass2.AnonymousClass1 */

                                public void onStop() {
                                    super.onStop();
                                    if (YYTrackSDK.mOnDestroyRecordListener != null) {
                                        YYTrackSDK.mOnDestroyRecordListener.result(true, "");
                                    }
                                }
                            };
                            YYTrackSDK.projection.registerCallback(YYTrackLibActivity.this.mCallback, null);
                            YYTrackSDK.imageReader = ImageReader.newInstance(YYTrackLibActivity.this.mWidthPixels, YYTrackLibActivity.this.mHeightPixels, 1, 15);
                            YYTrackLibActivity.this.mVirtualDisplay = YYTrackSDK.projection.createVirtualDisplay("shot", YYTrackLibActivity.this.mWidthPixels, YYTrackLibActivity.this.mHeightPixels, YYTrackLibActivity.this.dpi, 16, YYTrackSDK.imageReader.getSurface(), null, null);
                            YYTrackSDK.imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
                                public void onImageAvailable(ImageReader imageReader) {
                                    if (!YYTrackSDK.hasStart) {
                                        YYTrackSDK.hasStart = true;
                                        if (YYTrackSDK.mOnStrartRecordListener != null) {
                                            YYTrackSDK.mOnStrartRecordListener.result(true, "");
                                        }
                                    }
                                }
                            }, null);
                        }
                    }
                } catch (Exception e) {
                    LkxLog.d("YYTrackLibActivity startRecording error :"+e.getMessage());
                    e.printStackTrace();
                    YYTrackLibActivity.this.finishMethod();
                }
            }
        }, 500);
    }

    private void relealse() {
        try {
            VirtualDisplay virtualDisplay = this.mVirtualDisplay;
            if (virtualDisplay != null) {
                virtualDisplay.release();
                this.mVirtualDisplay = null;
            }
            if (YYTrackSDK.projection != null) {
                YYTrackSDK.projection.stop();
                YYTrackSDK.projection = null;
                YYTrackSDK.imageReader.close();
                YYTrackSDK.imageReader = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            YYTrackSDK.hasPermission = false;
            finishMethod();
        }
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onDestroy() {
        super.onDestroy();
        destroyRecord();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void destroyRecord() {
        try {
            EventBus.getDefault().unregister(this);
            stopService(new Intent(this, TrackScreenService.class));
            relealse();
            finishMethod();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void finishMethod() {
        try {
            YYScreenCutSDK.mIsFinish = true;
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
